﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;

/// <summary>
///ValiManage 的摘要说明
/// </summary>
public class ValiManage
{
    // Fields
    private Graphics g;
    private Bitmap image = new Bitmap((int) Math.Ceiling((double) 51.2), 20);

    // Methods
    public ValiManage()
    {
        this.g = Graphics.FromImage(this.image);
    }

    public Bitmap CreateCheckCodeImage(string checkCode)
    {
        if ((checkCode != null) && (checkCode.Trim() != string.Empty))
        {
            Random random = new Random();
            this.g.Clear(Color.White);
            for (int i = 0; i < 0x19; i++)
            {
                int num2 = random.Next(this.image.Width);
                int num3 = random.Next(this.image.Width);
                int num4 = random.Next(this.image.Height);
                int num5 = random.Next(this.image.Height);
                this.g.DrawLine(new Pen(Color.Silver), num2, num4, num3, num5);
            }
            Font font = new Font("Arial", 12f, FontStyle.Italic | FontStyle.Bold);
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, this.image.Width, this.image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            this.g.DrawString(checkCode, font, brush, (float) 2f, (float) 2f);
            for (int j = 0; j < 100; j++)
            {
                int x = random.Next(this.image.Width);
                int y = random.Next(this.image.Height);
                this.image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }
            this.g.DrawRectangle(new Pen(Color.Silver), 0, 0, this.image.Width - 1, this.image.Height - 1);
            //this.g.DrawRectangle(new Pen(Color.White), 0, 0, this.image.Width - 1, this.image.Height - 1);
        }
        return this.image;
    }

    public Bitmap CreateCheckImage(string codeStr)
    {
        if ((codeStr != null) && (codeStr.Trim() != string.Empty))
        {
            //this.g.Clear(Color.LightYellow);
            this.g.Clear(Color.White);
            Font font = new Font(FontFamily.GenericSerif, 16f, FontStyle.Bold, GraphicsUnit.Pixel);
            Random random = new Random();
            for (int i = 0; i < 4; i++)
            {
                string s = codeStr.Substring(i, 1);
                this.g.DrawString(s, font, new SolidBrush(Color.Black), (float) (i * 12), (float) random.Next(0, 5));
            }
            Pen pen = new Pen(new SolidBrush(Color.Black), 1f);
            for (int j = 0; j < 4; j++)
            {
                this.g.DrawLine(pen, new Point(random.Next(0, 0x31), random.Next(0, 0x13)), new Point(random.Next(0, 0x31), random.Next(0, 0x13)));
            }
        }
        return this.image;
    }

    public void Dispose()
    {
        this.g.Dispose();
        this.image.Dispose();
    }

    public string GenerateCheckCode()
    {
        string str = string.Empty;
        Random random = new Random();
        for (int i = 0; i < 4; i++)
        {
            int num = random.Next();
            str = str + ((char) (0x30 + ((ushort) (num % 10)))).ToString();
        }
        return str;
    }
}